package com.winz.cultureapp.activity;

import android.content.Intent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.winz.cultureapp.R;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 新闻资讯
 */

public class NewsDesActivity extends BaseAppCompatActivity {
    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.wv_news)
    WebView mWv_news;
    @BindView(R.id.pb_news)
    ProgressBar mPb_news;


    private String id ;
    private String href ;

    @Override
    protected int contentView() {
        return R.layout.activity_news_des;
    }

    @Override
    protected void initViews() {
        mWv_news.getSettings().setJavaScriptEnabled(true);//设置WebView支持JS
        mWv_news.getSettings().setDefaultTextEncodingName("utf-8");//设置解码时的字符集
        mWv_news.getSettings().setLoadWithOverviewMode(true);
        mWv_news.getSettings().setUseWideViewPort(true);
        mWv_news.getSettings().setSupportZoom(true);
        mWv_news.getSettings().setBuiltInZoomControls(true);
        mWv_news.getSettings().setDomStorageEnabled(true);
        mWv_news.getSettings().setBuiltInZoomControls(false);
        mWv_news.setWebViewClient(new WebViewClient() {
            /*
            此处能拦截超链接的url,即拦截href请求的内容.
            */
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                mWv_news.loadUrl(url);
//                if (url.startsWith("http")) {
//
////                    // 调用系统默认浏览器处理url
////                    view.stopLoading();
////                    view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
//                    return true;
//                }else {
//                    showMsg("该链接不是http");
//                }
                return true;
            }

        });
        mWv_news.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if(newProgress==100){
                    mPb_news.setVisibility(View.GONE);//加载完网页进度条消失

                }
                else{
                    mPb_news.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    mPb_news.setProgress(newProgress);//设置进度值

                }
            }
        });


    }
    @Override
    protected void loadDatas(Intent intent) {
        if (intent != null){
            if (intent.getExtras() != null){
                id = intent.getExtras().getString(Constants.KEY.NEWS.ID);
                href = intent.getExtras().getString(Constants.KEY.NEWS.HREF);
                String top_title = intent.getExtras().getString(Constants.KEY.NEWS.TOP_TITLE);

                mTv_top_title.setText(top_title);
                mWv_news.loadUrl(href);
            }
        }
    }


    @OnClick({R.id.fl_back})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.fl_back:
                finish();
                break;

        }
    }

}
